﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TV_2013.Handle;
using TV_2013.Model;

namespace TV_2013.View.Feature2.CompoundWord
{
    public partial class ViewCompoundWord : PhoneApplicationPage
    {
        ReadData readData = new ReadData("Data/DataCompoundWord.xml");
        CompoundWordModel compoundWord = new CompoundWordModel();
        private string currentLetter;
        private int n;

        public ViewCompoundWord()
        {
            InitializeComponent();
        }

        public Button CreateButton(String _content, int _tag, int _height, int _width,
            int _borderThickness, int _fontSize, 
            bool _isHasClick, bool _isEnabled, bool _isHitTestVisible)
        {
            Button btntxt = new Button();

            if (null != _content)
            {
                btntxt.Content = _content;
            }

            btntxt.Height = _height;
            btntxt.Width = _width;
            btntxt.FontSize = _fontSize;

            if (-1 != _tag)
            {
                btntxt.Tag = _tag;
            }

            if (true == _isHasClick)
            {
                btntxt.Click += btntxt_Click;
            }

            if (-1 != _borderThickness)
            {
                btntxt.BorderThickness = new System.Windows.Thickness(0, 0, 0, _borderThickness);
            }

            btntxt.IsEnabled = _isEnabled;
            btntxt.IsHitTestVisible = _isHitTestVisible;

            return btntxt;
        }
        
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string id = NavigationContext.QueryString["id"];
            n = Int32.Parse(NavigationContext.QueryString["n"]);
            compoundWord = readData.GetCompoundWordDataByAttribute(id, n);
            currentLetter = null;

            string comper = null;

            comper = GetLetter(compoundWord.ArrayText);
            comper = Anagram(comper);

            for (int i = 0; i < comper.Length; ++i)
            {
                panelRandomText.Children.Add(CreateButton(comper[i].ToString(), -1, 124, 124, 5, 60, true, true, true));
            }

            for (int i = 0; i < n; ++i)
            {
                if (compoundWord.ArrayText[i] != "/")
                {
                    panelResultText.Children.Add(CreateButton(null, 1, 124, 124, 5, 60, true, true, true));
                }
                else
                {
                    panelResultText.Children.Add(CreateButton(null, 0, 124, 40, 0, 60, false, false, false));
                }
            }
        }

        void btntxt_Click(object sender, RoutedEventArgs e)
        {
            Button btn = new Button();
            btn = (Button)sender;
            
            StackPanel panel = new StackPanel();
            panel = (StackPanel)btn.Parent;

            if ("panelRandomText" == panel.Name)
            {
                currentLetter = btn.Content.ToString();
                btn_currLetter.Content = currentLetter;
            }
            else
            {
                if ("panelResultText" == panel.Name)
                {
                    if (null != currentLetter)
                    {
                        btn.Content = currentLetter;
                    }
                }
            }
        }

        private void btn_play_Click(object sender, RoutedEventArgs e)
        {
            soundLesson.Source = new Uri(compoundWord.SoundUrl, UriKind.Relative);
        }

        private void btn_check_Click(object sender, RoutedEventArgs e)
        {
            string result = null;
            for (int i = 0; i < panelResultText.Children.Count; ++i)
            {
                Button btn = new Button();
                btn = (Button)panelResultText.Children[i];
                string temp = btn.Tag.ToString();
                if ((temp == "1") && (btn.Content != null))
                {
                    result += btn.Content.ToString();
                }
            }

            if ((result != null) && (result == TrueResult()))
            {
                soundLesson.Source = new Uri("/Sound/tiengvotay.wma", UriKind.Relative);
                soundLesson.Play();
                MessageBox.Show("Chúc mừng!");
                currentLetter = null;
                btn_currLetter.Content = currentLetter;
            }
            else
            {
                MessageBox.Show("Tiếc quá, sai rồi!");
                currentLetter = null;
                btn_currLetter.Content = currentLetter;
            }
        }

        private string TrueResult()
        {
            string result = null;
            for (int i = 0; i < n; ++i)
            {
                if ("/" != compoundWord.ArrayText[i])
                {
                    result += compoundWord.ArrayText[i];
                }
            }

            return result;
        }

        private string GetLetter(string[] _text)
        {
            string result = null;
            for (int i = 0; i < _text.Length; ++i)
            {
                result += _text[i];
            }

            for (int i = 0; i < result.Length; ++i)
            {
                if ('/' == result[i])
                {
                    result = result.Remove(i, 1);
                    --i;
                }
                else
                {
                    for (int j = i + 1; j < result.Length; ++j)
                    {
                        if (result[i] == result[j])
                        {
                            result = result.Remove(j, 1);
                            --j;
                        }
                    }
                }
            }

            return result;
        }

        private string Anagram(string _text)
        {
            string temp = _text;
            string result = null;
            int n = _text.Length;
            Random ran = new Random();

            for (int i = 0; i < n; ++i )
            {
                int j = (int)(ran.Next(0, temp.Length));
                result += temp[j];
                temp = temp.Remove(j, 1);
            }

            return result;
        }
    }
}